/*
作者：江怀玉
日期：2015-3-27
功能：自动完成选择信息
解决：
1、数据显示问题
2、可以搜索
3、数据显示
4、勾选
-------------------------------------
依赖:
jquery,
/scripts/jquery.data.js :fnGetData
---------------------------------------------------------*/
(function ($) {
    $.fn.datalist = function (options) {
        //debugger
        var _opt = !options ? { "entity": "V_User"} : options;
        var d = $(this).attr("displaymember"); //显示在控件中字段
        var f = $(this).attr("filter");        //过滤字段
        var t = $(this).attr("top");           //显示几行
        var l = $(this).attr("listMember");    //显示的列表字段
        var c = $(this).attr("isCheck");       //显示的列表字段
        var o = $(this).attr("order")==undefined?"":$(this).attr("order");
        var h = $(this).attr("header");        //显示表头
        var _l = l == undefined || l == "" ? [{ F: "caption", W: "250px"}] : eval(l); //显示的列表字段
        var _h = h == undefined || h == "" ? "" : eval(h);
        var arrayf = new Array();              //定义要过滤的字段组
        arrayf = f.split(',');
        var $data = $(this).attr("dataKey");

        var $this = this;
        var _data = null;
        var _smode = $(this).attr("mode");   //当前模式 /可选，多选，单选
        var _event = $(this).attr("event");
        var _CureentRow = null;              //当前行
        var _ds = null;                     //数据源
        var _c = $(this).attr("condition") == undefined ? "" : $(this).attr("condition"); //条件
        var defaults = {
            id: $(this).attr("id"),
            entity: $(this).attr("entity"), //对应实体 
            sort: $(this).attr("sort"),    //排序
            condition: $(this).attr("condition"), //条件
            filter: f == undefined || f == "" ? "caption" : f,        //过滤字段
            displaymember: d == undefined || d == "" ? "caption" : d, //显示字段
            top: t == undefined || t == "" ? "25" : t,                //
            list: _l,                                                 //显示列表
            event: $(this).attr("event")                             //事件方法           
        };


        function SetDataTable() {
            var o_value = "";
            var key = o_value;
            var condition = key == "" ? "" : getCondition(key, arrayf);
            condition = condition == "" ? _c : condition + " and " + _c;
            getData(defaults.entity, condition, o, createList);
        }

        //搜索条件
        function getCondition(key, _fields) {

            var _condition = "";
            for (i = 0; i < _fields.length; i++) {
                if (i == 0) {
                    _condition = _condition + " (" + _fields[i] + " like '%" + key + "%')  ";
                }
                else {
                    _condition = _condition + " or (" + _fields[i] + " like '%" + key + "%')  "
                }
            }
            return _condition;
        }

        function getData(entity, condition, sort, callback) {
            fnGetAsyncData(entity, condition, sort, "YES", "", callback);
        }




        //创建表
        var createList = function (rows) {
            //debugger
            _ds = rows;     //数据缓存到对象
            $($this).data($data, _ds);
            $this.html("");
            var html = "<table>";
            var _slist = defaults.list;

            if (rows == null || rows == undefined) {
                cols =
                html += "<tr><td colspan=\"" + _l.length + "\">没有数据...</td></tr>";
            } else {
                if (_h != "") {
                    html += "<tr class=\"tableHead\" onselectstart=\"return false\"style=\"top:e­xpression(document.getElementById('tC').scrollTop)\">";
                    if (c == "true") {
                        html += '<td style="width: ' + 1 + 'px;"><input type="checkbox" id="checkAll'+$this.attr("id")+'"/></td>';
                    }
                    for (var _index = 0; _index < _h.length; _index++) {
                        html += '<td style="width: ' + _h[_index].W + ';">' + _h[_index].Caption + '</td>';
                    }
                    html += "</tr>";
                }
                for (var i = 0; i < rows.length; i++) {
                    var list = rows[i];
                    html += "<tr index='" + i + "' class='row" + i % 2 + "'>";
                    if (c == "true") {
                        html += '<td style="width: ' + 1 + 'px;"><input type="checkbox"/></td>';
                    }
                    for (var _index = 0; _index < _slist.length; _index++) {
                        html += '<td style="width: ' + _slist[_index].W + ';">' + rows[i][_slist[_index].F] + '</td>';
                    }
                    html += "</tr>";
                }
            }
            html += "</table>"
            $this.html(html);
            $this.find("tr").click(rowClick);
        }


        //获取显示
        var getSelectedRows = function () {
            return "";
        }


        //单击行
        var rowClick = function () {
            //debugger
            $this.find("tr").removeClass("selected");
            $(this).addClass("selected");
            if (_event != "" && _event != undefined) {
                eval(_event);
            }
        }
        SetDataTable();
    }
})(jQuery);

//初始化控件
$(document).ready(function () {
    var controls = $(this).find('div');
    controls.each(function () {
        if ($(this).attr("ctrlType") == "datalist") {
            $(this).datalist();
        }
    });
});
/*
-------------------------------------
样例:
<div type="text" SValue="" SText="" id="Dept" value="" ctrlType="AutoComplete" SelectMode="s" entity="V_Dept" filter="DeptPath,name" valuemember="DeptPath" displaymember="name" listMember="[{F:'name',W:'150px'},{F:'DeptPath',W:'120px'}]" ><div/>
<div type="text" SValue="" SText="" id="UName" value="" ctrlType="AutoComplete" SelectMode="s" entity="DatUser" filter="LoginName,UName" valuemember="LoginName" displaymember="UName" listMember="[{F:'LoginName',W:'150px'},{F:'UName',W:'120px'}]"/>
-------------------------------------
*/

